package app.controllers.system;

import app.models.system.Resource;
import com.ifonly.activerecord.annotation.ARDao;
import com.ifonly.common.ApiMessage;
import com.ifonly.common.ztree.simple.SimpleZNode;
import com.ifonly.mvc.ActionKey;
import com.ifonly.mvc.Controller;

import java.util.List;

/**
 * @author ifonly
 * @version 1.0 2016-01-19 11:38
 * @since JDK 1.6
 */
public class ResourceController extends Controller {

    @ARDao
    private Resource dao;

    @ActionKey("")
    public void index() {
        render("index");
    }

    public void list() {
        List<SimpleZNode<Integer, Resource>> list = dao.list();
        renderJson(ApiMessage.success(list));
    }

    public void item() {
        int id = getInt(0);
        Resource resource = Resource.findByPrimaryKey(Resource.class, id);
        renderJson(ApiMessage.success(resource));
    }

    public void save() {
        Resource resource = getModel(Resource.class, "resource");
        this.dao.saveOrUpdate(resource);
        renderJson(ApiMessage.success(resource));
    }

    public void delete() {
        int id = getInt(0);
        dao.deleteById(id);
        renderJson(ApiMessage.success());
    }
}
